package com.example.fujia.androidapp;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.fujia.androidapp.activity.LoginActivity;
import com.example.fujia.androidapp.view.fragment.BlockFragment;
import com.example.fujia.androidapp.view.fragment.HomeFragment;
import com.example.fujia.androidapp.view.fragment.PersonFragment;
import com.example.fujia.androidapp.view.fragment.SearchFragment;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    // UI Object
    private TextView nbb_home;
    private TextView nbb_block;
    private TextView nbb_search;
    private TextView nbb_person;

    private FragmentManager fManager;

    // Fragment Object
    private HomeFragment fg1;
    private BlockFragment fg2;
    private SearchFragment fg3;
    private PersonFragment fg4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        // 底部导航栏设置
        this.setNavBottom();

        // 第一次进入跳转登录
        SharedPreferences sharedPreferences= getSharedPreferences("user", Activity.MODE_PRIVATE);
        if (sharedPreferences.getInt("userId", 0) == 0){
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    /**
     * 底部导航栏设置
     * UI组件初始化与事件绑定
     * */
    private void setNavBottom(){
        this.nbb_home = (TextView) findViewById(R.id.nbb_home);
        this.nbb_block = (TextView) findViewById(R.id.nbb_block);
        this.nbb_search = (TextView) findViewById(R.id.nbb_search);
        this.nbb_person = (TextView) findViewById(R.id.nbb_person);

        this.fManager = getFragmentManager();


        this.nbb_home.setOnClickListener(new homeOnClick());
        this.nbb_block.setOnClickListener(new blockOnClick());
        this.nbb_search.setOnClickListener(new searchOnClick());
        this.nbb_person.setOnClickListener(new personOnClick());

        this.nbb_home.performClick();
    }

    /**
     * 主页点击类
     * */
    class homeOnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            FragmentTransaction fTransaction = fManager.beginTransaction();
            hideAllFragment(fTransaction);

            setSelected();
            nbb_home.setSelected(true);
            if (fg1 == null){
                fg1 = new HomeFragment();
                fTransaction.add(R.id.ly_content, fg1);
            } else {
                fTransaction.show(fg1);
            }

            fTransaction.commit();
        }
    }

    /**
     * 版区点击类
     * */
    class blockOnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            FragmentTransaction fTransaction = fManager.beginTransaction();
            hideAllFragment(fTransaction);

            setSelected();
            nbb_block.setSelected(true);
            if (fg2 == null){
                fg2 = new BlockFragment();
                fTransaction.add(R.id.ly_content, fg2);
            } else {
                fTransaction.show(fg2);
            }

            fTransaction.commit();
        }
    }

    /**
     * 发现点击类
     * */
    class searchOnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            FragmentTransaction fTransaction = fManager.beginTransaction();
            hideAllFragment(fTransaction);

            setSelected();
            nbb_search.setSelected(true);
            if (fg3 == null){
                fg3 = new SearchFragment();
                fTransaction.add(R.id.ly_content, fg3);
            } else {
                fTransaction.show(fg3);
            }

            fTransaction.commit();
        }
    }

    /**
     * 个人中心点击事件类
     * */
    class personOnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            FragmentTransaction fTransaction = fManager.beginTransaction();
            hideAllFragment(fTransaction);

            setSelected();
            nbb_person.setSelected(true);
            if (fg4 == null){
                fg4 = new PersonFragment();
                fTransaction.add(R.id.ly_content, fg4);
            } else {
                fTransaction.show(fg4);
            }

            fTransaction.commit();
        }
    }

    /**
     * 重置所有文本的选中状态
     * */
    private void setSelected(){
        nbb_home.setSelected(false);
        nbb_block.setSelected(false);
        nbb_search.setSelected(false);
        nbb_person.setSelected(false);
    }

    /**
     * 隐藏所有Fragment
     * */
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
        if(fg4 != null)fragmentTransaction.hide(fg4);
    }

}
